//
// Created by wangjunjun on 2024/2/4.
//

#include "keywidget.h"
#include <qdebug.h>
#include <QDateTime>
#include <QCryptographicHash>
#include "src/util/crc/crc.h"
KeyWidget::KeyWidget(QWidget *parent)
        : QWidget{parent}
{
    setFocusPolicy(Qt::StrongFocus);;
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &KeyWidget::countKeys);
}

void KeyWidget::onCountKeysMsg()
{
    QString udKeys;  //+代表向前， -代表向后
    QString rfKeys;  //+代表向左， -代表后右
    QString keyMsg;
    if(upCount >0)
    {
        udKeys = "+" + QString::number(double(upCount)*0.1, 'f', 2);
    }else if (downCount >0)
    {
        udKeys = "-" + QString::number(double(downCount)*0.1, 'f', 2);
    }else
    {
        udKeys ="0";
    }
    //
    if(leftCount >0)
    {
        rfKeys = "+" + QString::number(double(leftCount)*0.1, 'f', 2);
    }else if (rightCount >0)
    {
        rfKeys = "-" + QString::number(double(rightCount)*0.1, 'f', 2);
    }else
    {
        rfKeys ="0";
    }
    qint64  timeStamp = QDateTime::currentMSecsSinceEpoch();
    qDebug() << "Current Time:" << timeStamp;
    keyMsg= udKeys + " " + rfKeys  + " " + QString::number(QDateTime::currentMSecsSinceEpoch());
    qDebug() << "KeyWidget::onCountKeysMsg :" << keyMsg;

//    QString udKeys = QString("%1").arg(static_cast<double>(upCount - downCount) * 0.1, 0, 'f', 2);
//    QString rfKeys = QString("%1").arg(static_cast<double>(leftCount - rightCount) * 0.1, 0, 'f', 2);
//    QString keyMsg = udKeys + " " + rfKeys;
    //添加CRC校验
    QString crc = CalculatedCRC::GetInstance().calculateCRC32(keyMsg);
    QString dataWithCRC = keyMsg +" "+ crc;
    emit senKeyMsg(dataWithCRC);

}
//param：统计按键按下次数
void KeyWidget::countKeys()
{
    upCount +=QApplication::keyboardModifiers()& Qt::Key_Up?1:0;
    downCount+= QApplication::keyboardModifiers()& Qt::Key_Down?1:0;
    leftCount+=QApplication::keyboardModifiers()& Qt::Key_Left?1:0;
    rightCount+=QApplication::keyboardModifiers()& Qt::Key_Right?1:0;
    onCountKeysMsg();
 }
//param[in] event:键盘按键按下触发事件，按键包括长按和短按
void KeyWidget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
        case Qt::Key_Up:
            if (!event->isAutoRepeat() && event->key() == Qt::Key_Up) {
                if (!timer->isActive()) {
                    timer->start(10); // 100 milliseconds interval for counting
                    upCount = 1;
                }
            }
            if(event->isAutoRepeat() && event->key() == Qt::Key_Up){
                if (!timer->isActive()){
                    timer->start(10);
                }
                upCount++;
                if(upCount  > Speed_Max){
                    upCount = Speed_Max;
                }
            }
            break;
        case Qt::Key_Down:
            if (!event->isAutoRepeat() && event->key() ==  Qt::Key_Down) {
                if (!timer->isActive()) {
                    timer->start(10);
                    downCount = 1;
                }
            }
            if (event->isAutoRepeat() && event->key() == Qt::Key_Down){
                if (!timer->isActive()){
                    timer->start(10);
                }
                downCount++;
                if( downCount  > Speed_Max){
                    downCount = Speed_Max;
                }
            }
            break;
        case Qt::Key_Left:
            if (!event->isAutoRepeat()  && event->key() == Qt::Key_Left) {
                if (!timer->isActive()) {
                    timer->start(10);
                    leftCount = 1;
                }
            }
            if (event->isAutoRepeat() && event->key() == Qt::Key_Left){
                if (!timer->isActive()){
                    timer->start(10);
                }
                leftCount++;
                if( leftCount  > Speed_Max){
                    leftCount = Speed_Max;
                }
            }
            break;
        case Qt::Key_Right:
            if (!event->isAutoRepeat() && event->key() == Qt::Key_Right) {
                if (!timer->isActive()) {
                    timer->start(10);
                    rightCount= 1;
                }
            }
            if (event->isAutoRepeat() && event->key() == Qt::Key_Right){
                if (!timer->isActive()){
                    timer->start(10);
                }
                rightCount++;
                if( rightCount  > Speed_Max){
                    rightCount = Speed_Max;
                }
            }
            break;
        default:
            QWidget::keyPressEvent(event);
            break;
    }
}
//para[in] event:键盘按键弹起触发事件
void KeyWidget::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key()) {
        case Qt::Key_Up:
            if (!event->isAutoRepeat()) {
                if (timer->isActive()) {
                    upCount = 0;
                }
            }

            break;
        case Qt::Key_Down:
            if (!event->isAutoRepeat()) {
                if (timer->isActive()) {
                    downCount = 0;
                }
            }

            break;
        case Qt::Key_Left:
            if (!event->isAutoRepeat()) {
                if (timer->isActive()) {
                    leftCount = 0;
                }
            }

            break;
        case Qt::Key_Right:
            if (!event->isAutoRepeat()) {
                if (timer->isActive()) {
                    rightCount = 0;
                }
            }

            break;
        default:
            QWidget::keyPressEvent(event);
            break;
    }
    if(upCount ==0 && downCount ==0 &&leftCount ==0 && rightCount ==0 )
    {
        if (timer->isActive()) {
            countKeys();
            timer->stop();
        }
    }

}

bool KeyWidget::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        switch (keyEvent->key()) {
            case Qt::Key_Up:
                upCount++;
                break;
            case Qt::Key_Down:
                downCount++;
                break;
            case Qt::Key_Left:
                leftCount++;
                break;
            case Qt::Key_Right:
                rightCount++;
                break;
            default:
                break;
        }
    }
    return QWidget::eventFilter(obj, event);
}
